package com.ruoyi.utils;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author zhouzhixiong
 * @Date 2024/3/16 9:38
 */
@Component
public class DeployService {

    @Resource
    private RepositoryService repositoryService;

    /**
     * 从指定目录部署指定的BPMN流程定义文件
     *
     * @param bpmnFilename    文件名
     * @param deploymentName  部署名称
     */
    public String deployment(String bpmnFilename, String deploymentName) throws IOException {
        ClassPathResource bpmnResource = new ClassPathResource("bpmn/" + bpmnFilename);
        Deployment deployment = repositoryService.createDeployment()
                .addInputStream(bpmnFilename, bpmnResource.getInputStream())
                .name(deploymentName)
                .deploy();
        return deployment.getId();
    }


    /**
     * 删除指定的流程部署
     *
     * @param deploymentId 部署ID
     * @param cascade 是否级联删除
     */
    public void deleteDeployment(String deploymentId, boolean cascade) {
        repositoryService.deleteDeployment(deploymentId, cascade);
    }
}
